/**
 * volatile 完成内存可见性
 */
public class VolatileExample {
    private static volatile boolean flag = true;

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            while (flag) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() +
                        " : It doesn't matter if you make mistakes. Learning is progress~");
            }
            try {
                System.out.println(Thread.currentThread().getName() +
                        " : I am leave in just one second!!");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I am leave~");
        }, "bala_bala");

        Thread thread2 = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("change flag to \"false\"");
            flag = false;
        });

        thread1.start();
        thread2.start();
    }
}
